﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.SharePoint.WebControls;
using Microsoft.Reporting.WebForms;
using System.IO;
using System.Collections;

using JM.JCY.Common;
using JM.JCY.Common.Utilities;
using System.Web.Security;
using Microsoft.SharePoint.Administration;
using JM.JCY.Proxy;
using JM.JCY.BusinessEntity;
using NCS.IConnect.ApplicationContexts;

namespace JM.JCY.SharePointComponent.PerformanceArchives.LiangHuaStatisticsView
{
    public partial class LiangHuaStatisticsViewUserControl : UserControl
    {
        public int _caseId
        {
            set { ViewState["_caseId"] = value; }
            get { return (int)ViewState["_caseId"]; }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.tbSearch.Visible = false;
                using (PerformanceCaseProxy proxy = new PerformanceCaseProxy())
                {
                    if (Request.QueryString["CaseId"] != null)
                    {
                        _caseId = Int32.Parse(Request.QueryString["CaseId"].ToString());
                        GetLiangHuaReport(_caseId);
                    }
                    
                }
            }

        }

        private void GetLiangHuaReport(int caseId)
        {
            using (PerformanceCaseProxy proxy = new PerformanceCaseProxy())
            {
                var sourceList = proxy.GetLiangHuaStatistics(caseId, string.Empty, string.Empty, string.Empty);
                sourceList.AcceptChanges();
                List<PerformanceCaseDataSet.T_LiangHuaStatisticsRow> sourceListFilter = sourceList.ToList();

                if (sourceList.Count > 0)
                {
                    this.lblNoMsg.Visible = false;
                    this.rvLiangHua.Visible = true;

                    ReportDataSource source = new ReportDataSource("DataSet1", sourceListFilter);
                    this.rvLiangHua.LocalReport.ReportPath = Server.MapPath("~/_layouts/Template/LiangHuaStatistics.rdlc");
                    this.rvLiangHua.LocalReport.DataSources.Clear();
                    this.rvLiangHua.LocalReport.DataSources.Add(source);

                    List<ReportParameter> p = new List<ReportParameter>();
                    // judge the title
                    p.Add(new ReportParameter("Title", GetTitle(sourceList)));

                    this.rvLiangHua.LocalReport.SetParameters(p);
                    this.rvLiangHua.LocalReport.Refresh();
                }
                else
                {
                    this.lblNoMsg.Visible = true;
                    this.rvLiangHua.Visible = false;
                }
            }


        }

        private string GetTitle(PerformanceCaseDataSet.T_LiangHuaStatisticsDataTable dt)
        {
            string title = string.Empty;
            string year = dt[0].Year;
            string dept = dt[0].Dept;
            string name = dt[0].Name;

            // year
            if (!string.Empty.Equals(year))
                title += year + "年度";
            else
                title += "近三年年度";
            //Dept
            if (!string.Empty.Equals(dept))
                title += dept;
            //Name
            if (!string.Empty.Equals(name))
                title += name;
            //Rest
            title += "工作目标量化考评得分表";

            return title;
        }

        protected void imgSubmit_Click(object sender, ImageClickEventArgs e)
        {
           
        }

        protected void imgCancel_Click(object sender, ImageClickEventArgs e)
        {

        }
    }
}
